<script>
import { tools } from "./utils/util";
import myRequest, * as request from "@/utils/api.js";
import * as api from "@/api/home.js";
// #ifdef H5
var userAgent = navigator.userAgent;
// console.log(userAgent, /miniProgram/i.test(userAgent) && /micromessenger/i.test(userAgent));
if (/miniProgram/i.test(userAgent) && /micromessenger/i.test(userAgent)) {
	// 微信小程序 JS-SDK 如果不需要兼容微信小程序，则无需引用此 JS 文件。
	document.write('<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"><\/script>');
}
document.write(`<script type="text/javascript" src="${tools.imgPath}index/hybrid_html_uni.webview.1.5.5.js"><\/script>`);
// #endif
let isOpenUpdateDialog = false;
export default {
	onLaunch: function (options) {
		console.log("App Launch", options);
		// 有邀请码
		if (options.query.gid) {
			uni.setStorageSync("sharerInfo", options.query.gid);
		}

		this.initSystemInfo();
		this.initFontList();
		// #ifdef H5
		this.initWebInfo();
		// #endif

		// #ifdef MP-WEIXIN
		this.getWxAppId();
		// #endif

		// #ifdef APP
		this.getAPPInfo();
		// #endif
	},
	onShow: async function () {
		console.log("App Show");
		// #ifdef MP-WEIXIN
		//解决IOS播放没声音
		wx.setInnerAudioOption({
			obeyMuteSwitch: false
		});
		// #endif
		// #ifdef H5
		var ua = navigator?.userAgent.toLowerCase();
		uni.setStorageSync("ua", ua);
		/* if (ua.indexOf("micromessenger") != -1) {
			uni.showModal({
				title: "提示",
				content: "这是微信浏览器",
				success: function (res) {
					if (res.confirm) {
						console.log("用户点击确定");
					} else if (res.cancel) {
						console.log("用户点击取消");
					}
				}
			});
		} else {
			uni.showModal({
				title: "提示",
				content: "这不是微信浏览器",
				success: function (res) {
					if (res.confirm) {
						console.log("用户点击确定");
					} else if (res.cancel) {
						console.log("用户点击取消");
					}
				}
			});
		} */
		// #endif
		// #ifdef APP
		if (!isOpenUpdateDialog) {
			isOpenUpdateDialog = true;
			var req = {
				//升级检测数据
				appid: plus.runtime.appid,
				version: plus.runtime.version,
				fileType: "apk",
				appDir: `${tools.appName}/${tools.merchantId}`,
			};
			let name = `${req.appid}__V${req.version}.${req.fileType}`;
			let res = await request.post("/autoUpdate", req, {}, "https://zp.yainoo.com/audio_fft/");
			// let res = await request.post("/autoUpdate", req, {}, "http://192.168.124.53:3000/");
			console.log("升级检测数据", res, name);
			/* {
					"data": {
						version: JSON.parse(fileInfo).versionName,
						downloadUrl: downloadUrl + fileName
					},
					"code": 10200,
					"success": true,
					"message": "操作成功"
			} */
			if (res && res.data && res.data.downloadUrl) {
				let downloadUrl = res.data.downloadUrl;
				let version = res.data.version;
				let reqVerArr = req.version.split(".");
				let resVerArr = version.split(".");
				console.log({
					reqVerArr,
					resVerArr
				});
				let isUpdate = resVerArr.some((e, i) => resVerArr[i] > reqVerArr[i]);
				if (isUpdate) {
					uni.showModal({
						title: "发现新版本",
						content: "是否更新到" + version + "版本？",
						success: function (res) {
							isOpenUpdateDialog = false;
							if (res.confirm) {
								// 新版本提示用户下载
								uni.showLoading({
									title: "正在下载更新..."
								});
								// 使用plus.runtime.openURL下载APP
								plus.runtime.openURL(downloadUrl);
							} else if (res.cancel) {
								isOpenUpdateDialog = true;
							}
						},
						fail(err) {
							console.log(err);
							isOpenUpdateDialog = false;
						}
					});
				}
			}
		}
		// #endif
	},
	onHide: function () {
		console.log("App Hide");
	},
	methods: {
		// 获取系统info
		initSystemInfo() {
			const that = this;
			return new Promise((resolve, reject) => {
				uni.getSystemInfo({
					success(payload) {
						uni.setStorageSync("systemInfo", payload);
						resolve(payload);
					}
				});
			});
		},
		// fontList初始化
		initFontList() {
			return;
			return api.getFont().then((res) => {
				if (!res.data) return;
				res.data.map((fontobj) => {
					tools.loadFont_wx(fontobj.fontName, fontobj.fontUrl);
				});
			});
		},

		// 获取appInfo
		getAPPInfo() {},
		// 获取网站信息
		initWebInfo() {
			let link = document.querySelector('link[rel*="icon"]');
			if (!link) {
				link = document.createElement("link");
				link.setAttribute("rel", "icon");
				document.querySelector("head").appendChild(link);
			}
			let domainName = location.host;
			domainName = "ai.yainoo.com";
			// domainName = 'ai.lsjz-ai.com';
			// domainName = 'ai.eduoyun.cn';
			// domainName = "szr.guizhouxumao.com";
			// domainName = 'truebirdai.com';
			// domainName = 'www.truebirdai.com';
			if (domainName != "ai.yainoo.com") {
				api.getWebInfo({ domainName: domainName.replace(/^www./, "") }).then((res) => {
					console.log("getWebInfo", res);
					if (!res) return;
					// document.title = res.data.webTitle;
					this.$store.commit("changeWebInfo", res.data);
					this.$nextTick(() => {
						let currentIcon = res.data.customerService;
						link.href = currentIcon;
					});
				});
			} else {
				// this.$store.commit('changeWebInfo', {ipAddress:'ai.yainoo.com'});
				link.href = "https://ai.yainoo.com/32px32px.png";
				// document.title = tools.companyName + "-国内专业的一站式AIGC数字人服务平台-让影子为您代言";
			}
		},
		// 获取wxAppId
		getWxAppId() {
			const accountInfo = uni.getAccountInfoSync();
			const appid = accountInfo.miniProgram.appId;
			console.log("获取wxAppId", appid);
			uni.setStorageSync("wxAppid", appid);
		}
	}
};
</script>

<style lang="scss">
/*每个页面公共css */
* {
	scrollbar-width: none;
	/* Firefox */
	-ms-overflow-style: none;
	/* IE 10+ */
	margin: 0vw;
	padding: 0vw;
}

*::-webkit-scrollbar {
	display: none;
}
button {
	background: transparent;
	color: #000;
	margin: 0vw;
	padding: 0vw;
}
</style>
